#include "stringutil.h"
#include <sstream>
#include <cstdio>
#include <cctype>
string trimmed(string const& str , char const* sepSet)
{
	string::size_type const first = str.find_first_not_of(sepSet);
	return ( first==string::npos )
		? string()
		: str.substr(first, str.find_last_not_of(sepSet)-first+1);
}

void split(const string &str,vector<string> &splited,char const* sepSet)
{
	string::size_type pos;
	string::size_type prePos=0;
	splited.clear();
	while((pos=str.find_first_of(sepSet,prePos))!=string::npos)
	{
		splited.push_back(str.substr(prePos,pos-prePos));
		prePos=pos+1;
	}
	if(prePos<str.length())
	{
		splited.push_back(str.substr(prePos));
	}
}

string int2str(int value)
{
	stringstream ss;
	ss<<value;
	string str;
	ss>>str;
	return str;
}

int str2int(const string &str)
{
	stringstream ss;
	ss<<str;
	int value;
	ss>>value;
	return value;
}

string int2hex(int value)
{
	char buf[MAX_NUMLENGTH]={0};
	sprintf_s(buf,"%x",value,MAX_NUMLENGTH);
	return string(buf);
}

int hex2int(const string &str)
{
	int ivalue=0;
	for(string::const_iterator iter=str.begin();iter!=str.end();++iter)
	{
		char ch=*iter;
		if(ch>='0'&&ch<='9')
		{
			ivalue=ivalue*16+ch-'0';
		}
		else if(ch>='a'&&ch<='f')
		{
			ivalue=ivalue*16+ch-'a'+10;
		}
		else if(ch>='A'&&ch<='F')
		{
			ivalue=ivalue*16+ch-'A'+10;
		}
	}
	return ivalue;
}
string lowcase(const string &str)
{
	string low;
	for(int i=0;i<str.size();++i)
	{
		low+=tolower(str.at(i));
	}
	return low;
}
